/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionabsence.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author huynal
 */
public class PresenceBDD {
    
   public PresenceBDD() {
       
   }

   public static Presence trouverParId(int id) throws SQLException, Exception {
      Presence presence = new Presence();
      String sql = "SELECT * FROM PRESENCE WHERE ID = ?";
      PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      pst.setInt(1, id);
      if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            if (resultat.first() == true) {
                presence.init(resultat);
            } else {
                throw new Exception("Presence inconnu");
            }
            resultat.close();
        }
        return presence;
   }
   
      public static Boolean trouverParEtudiantetAgenda(int e ,int a) throws SQLException, Exception {
      String sql = "SELECT * FROM PRESENCE WHERE ETUDIANT_ID=? AND AGENDA_ID=?";
      PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      pst.setInt(1, e);
      pst.setInt(2, a);
      if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            if (resultat.first() == true) {
                return true;
            } 
            resultat.close();
        }
      return false;
   }
      
    public static Presence trouverParEtudiantetAgenda1(int e ,int a) throws SQLException, Exception {
      Presence presence = new Presence();
      String sql = "SELECT * FROM PRESENCE WHERE ETUDIANT_ID=? AND AGENDA_ID=?";
      PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      pst.setInt(1, e);
      pst.setInt(2, a);
        if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            if (resultat.first() == true) {
                presence.init(resultat);
            }
            resultat.close();
        }
        return presence;
   }
  
    
    public static Boolean supprimer(int id) throws SQLException, Exception {
      String sql = "DELETE PRESENCE WHERE ID=?";
      PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      pst.setInt(1, id);
      if (pst.execute()){
          return true;
      }
      return false;
   }
    
    public static Boolean ajouter(int e ,int a) {
      /*String sql = "INSERT INTO PRESENCE  VALUES (S_PRESENCE.nextval, ?, ?)*/
      //PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      //PreparedStatement pst = Connection.getBDD().prepareStatement(sql);
      //pst.setInt(1, e);
      //pst.setInt(2, a);
      try{
           
        Statement pst= Connection.getBDD().createStatement();
        pst.executeUpdate("INSERT INTO PRESENCE  VALUES (S_PRESENCE.nextval,"+e+","+a+")");
      } catch(SQLException e1){
          System.out.println(e1);
      }
      
      return true;
      /*
      if (pst.execute()){
          return true;
      }
      return false;
      
      */
   }
}
